/**
*    Copyright (C) 2008  Chase Kernan 
*    chase.kernan@gmail.com
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
**/

package com.chasekernan.hxnova.componenteditor;

import com.chasekernan.hxnova.core.components.Hull;
import flash.events.Event;
import org.aswing.event.FrameEvent;
import org.aswing.JFrame;
import org.aswing.JLabel;
import org.aswing.JAdjuster;
import org.aswing.BorderLayout;
import org.aswing.geom.IntDimension;
import org.aswing.geom.IntPoint;
import com.chasekernan.hxnova.core.components.ComponentTypeSet;
import com.chasekernan.hxnova.utils.Rectangle;
import org.aswing.geom.IntRectangle;
import com.chasekernan.hxnova.utils.Vector;
import flash.events.Event;
import com.chasekernan.hxnova.componenteditor.CloseEvent;

/**
    A dragable representation of a hull part.
**/
class HullEditorObject < T > extends JFrame {
    
    public static var DEFAULT_DIMENSIONS = new Vector(100, 100);
    public static var DEFAULT_LOCATION = new Vector(100, 100);
    public static var MINIMUM_DIMENSIONS = new Vector(50, 25);
    
    private var data : T;
    private var centerLabel : JLabel;
    private var adjuster : JAdjuster;
    
    /**
        Creates a new hull editor object with the object given.
    **/
    public function new(parent : Dynamic, object : T) {
        super(parent);
        
        //this just sets the title bar to only have a close button, but still
        //allows the window to be resized.
        var titleBar = getTitleBar();
        titleBar.setRestoreButton(null);
        titleBar.setIconifiedButton(null);
        titleBar.setMaximizeButton(null);
        
        centerLabel = new JLabel("");
        adjuster = new JAdjuster();
        adjuster.setMinimum(1);
        
        var contentPane = getContentPane();
        contentPane.append(centerLabel);
        contentPane.append(adjuster, BorderLayout.SOUTH);
        
        show();
		
		var me = this;
		addEventListener(FrameEvent.FRAME_CLOSING, function(?e : Dynamic) {
			me.dispatchEvent(new CloseEvent(me));
		});
    }
    
    /**
        Loads the data from the given object.
    **/
    public function loadFromObject(object : T, bounds : IntRectangle) {
        if (object == null) throw "Part cannot be null.";
        data = object;
    }
    
    /**
        Saves the data to the object stored by this editor.
        This should be called either every frame or when the hull is saved.
    **/
    public function saveToObject(bounds : IntRectangle) { }
    
    /**
        Returns the object that this is editing.
    **/
    public function getObject() : T {
        return data;
    }
    
    /**
        Checks that this editor is within the bounds given.
    **/
    public function checkBoundsAndSave(bounds : IntRectangle) {
        if (getWidth() > bounds.width) {
            setWidth(bounds.width);
            setX(bounds.x);
        } else {
            if (getX() < bounds.x) setX(bounds.x);
            
            var right = bounds.x + bounds.width;
            if (getWidth() + getX() > right) setX(right - getWidth());
        }
        
        if (getHeight() > bounds.height) {
            setHeight(bounds.height);
            setY(bounds.y);
        } else {
            if (getY() < bounds.y) setY(bounds.y);
            
            var bottom = bounds.y + bounds.height;
            if (getHeight() + getY() > bottom) setY(bottom - getHeight());
        } 
		
		saveToObject(bounds);
    }
	
	public override function tryToClose() {
		super.tryToClose();
	}
}